fast-sort
Fast-sort is a lightweight (850 bytes gzip), zero-dependency sorting library with TypeScript support.
Its easy-to-use and flexible syntax, combined with incredible speed , make it a top choice for developers seeking efficient, reliable, and customizable sorting solutions.
Quick examples
import { sort } from 'fast-sort';
const ascSorted = sort([1,4,2]).asc();
const descSorted = sort([1, 4, 2]).desc();
const sorted = sort(users).desc(u => u.firstName);
const sorted = sort(users).asc([
u => u.firstName,
u => u.lastName
]);
const sorted = sort(users).by([
{ asc: u => u.firstName },
{ desc: u => u.address.city }
]);
const sorted = sort(repositories).desc(r => r.openIssues + r.closedIssues);
const sorted = sort(users).asc('firstName');
Fore more examples check unit tests.
In place sorting
Fast-sort provides an inPlace sorting option that mutates the original array instead of creating a new instance, resulting in marginally faster and more memory-efficient sorting. However, both the inPlaceSort and default sort methods offer exactly the same functionality.
const { sort, inPlaceSort } = require('fast-sort');
const array = [3, 1, 5];
const sorted = sort(array).asc();
inPlaceSort(array).asc();
Natural sorting / Language sensitive sorting
By default fast-sort
is not doing language sensitive sorting of strings.
e.g 'image-11.jpg'
will be sorted before 'image-2.jpg'
(in ascending sorting).
We can provide custom Intl.Collator comparer to fast-sort for language sensitive sorting of strings.
Keep in mind that natural sort is slower then default sorting so recommendation is to use it
only when needed.
import { sort, createNewSortInstance } from 'fast-sort';
const testArr = ['image-2.jpg', 'image-11.jpg', 'image-3.jpg'];
sort(testArr).desc();
sort(testArr).by({
desc: true,
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
naturalSort(testArr).asc();
naturalSort(testArr).desc();
NOTE: It's known that Intl.Collator
might not sort null
values correctly so make sure to cast them to undefine
as described in the following issue
https://github.com/snovakovic/fast-sort/issues/54#issuecomment-1072289388
Custom sorting
Fast sort can be tailored to fit any sorting need or use case by:
- creating custom sorting instances
- overriding default comparer in
by
sorter - custom handling in provided callback function
- combination of any from above
For example we will sort tags
by "custom" tag importance (e.g vip
tag is of greater importance then captain
tag).
import { sort, createNewSortInstance } from 'fast-sort';
const tags = ['influencer', 'unknown', 'vip', 'captain'];
const tagsImportance = {
vip: 3,
influencer: 2,
captain: 1,
};
const descTags = sort(tags).desc(tag => tagImportance[tag] || 0);
const tagSorter = createNewSortInstance({
comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0),
inPlaceSorting: true,
});
tagSorter(tags).asc();
tagSorter(tags).desc();
const defaultSort = sort(tags).asc();
More examples
sort(null).asc();
sort(33).desc();
const addresses = [{ city: 'Split' }, { city: undefined }, { city: 'Zagreb'}];
sort(addresses).asc(a => a.city);
sort(addresses).desc(a => a.city);
Migrating from older versions
Documentation for v2 and older versions is available here.
For migrating to v3 you can reference CHANGELOG for what has been changed.
Benchmark
Five different benchmarks have been created to get better insight of how fast-sort perform under different scenarios.
Each benchmark is run with different array sizes raging from small 100 items to large 100 000 items.
Every run of benchmark outputs different results but the results are constantly showing better scores compared to similar popular sorting libraries.
Benchmark scores
Benchmark has been run on:
- 16 GB Ram
- Intel® Core™ i5-4570 CPU @ 3.20GHz × 4
- Ubuntu 16.04
- Node 8.9.1
Independent benchmark results from MacBook Air can be found in following PR:
https://github.com/snovakovic/fast-sort/pull/48
Running benchmark
To run benchmark on your PC follow steps from below
- git clone https://github.com/snovakovic/fast-sort.git
- cd fast-sort/benchmark
- npm install
- npm start
In case you notice any irregularities in benchmark or you want to add sort library to benchmark score
please open issue here